package com.dayuanit.dy17.mix.thread;

import java.util.concurrent.*;

public class TestThreadPoolCallable {
    private static final ThreadPoolExecutor threadPool = new ThreadPoolExecutor(5,
            5,
            60,
            TimeUnit.SECONDS,
            new ArrayBlockingQueue<>(5));

    public static void main(String[] args) throws ExecutionException, InterruptedException, TimeoutException {
        //带有返回值的线程池任务

        Callable tast = new Callable() {
            @Override
            public Object call() throws Exception {
                System.out.println("task call()");
                Thread.sleep(10 * 1000);
                return "ok";
            }
        };

        Future taskFuture = threadPool.submit(tast);

//        Object result = taskFuture.get();//等待任务结束 才会有返回值
        Object result = taskFuture.get(5, TimeUnit.SECONDS);//超时时间，可以防止死锁

        System.out.println("result=" + result);
    }
}
